<?php
namespace Index\Controller;
use Think\Controller;
//use Think\Verify;
class AttentionController extends Controller{
    /**
     * 关注问题
     */
    public function indexAction(){
        $qid = I('get.id');
        $uid = I('session.uid');
        var_dump($qid,$uid);
        $model = M();
        $sql = "select * from favorite WHERE qid='{$qid}' AND uid = '{$uid}'";
        $exist = $model->query($sql);
        var_dump($exist);
        if(empty($exist)){
            $sql = "insert into favorite (qid,uid) VALUE ('$qid','$uid')";
            $model ->execute($sql);
            $sql = "select attention from question WHERE id = $qid";
            $model ->query($sql);
            $attention = $attention[0]['attention'] + 1;
            $sqlattention = "update question set attention = '$attention' WHERE id = $qid";
            $model->execute($sqlattention);
            $this->success('关注成功',U("Index/people/index/id/$qid"),2);

        }else{
            $sql = "delete from favorite WHERE fid = $qid AND uid = $uid";
            $model->execute($sql);
            $sql = "select attention from question WHERE id = $qid";
            $model->query($sql);
            $attention = $attention[0]['attention'] - 1;
            $sqlattention = "update question set attention = '$attention' WHERE id = $qid";
            $model->execute($sqlattention);
            $this->success('取消成功',U("Index/people/index/id/$qid"),2);

        }









//
//        if(empty($exist)){
//            $sql = "insert into focus (fid,uid) VALUE ('$fid','$uid')";
//            $model ->execute($sql);
//            $sql = "select attention from question WHERE id = $fid";
//            $attention = $model->query($sql);
//            $attention = $attention[0]['attention'] + 1;
//            $sqlattention = "update question set attention = '$attention' WHERE id = $fid";
//            $model->execute($sqlattention);
//            $this->success('关注成功',U("Index/people/index/id/$fid"),2);
//        }else{
//            $sql = "delete from focus WHERE fid = $fid AND uid = $uid";
//            $model->execute($sql);
//            $sql = "select attention from question WHERE id=$fid";
//            $attention = $model->query($sql);
//            $attention = $attention[0]['attention'] - 1;
//            $sqlattention = "update question set attention = '$attention' WHERE id = $fid";
//            $model->execute($sqlattention);
//            $this->success('取消成功',U("Index/people/index/id/$fid"),2);
//
//
//
//        }
//
    }


    /**
     * 关注人
     */
    public function userconcernAction(){
        $uid = I('session.uid');
        $fid = I('get.id');
        var_dump($uid,$fid);
        $model = M();
        $exist = $model->query("select * from focus WHERE uid='{$uid}' AND fid='{$fid}'");
        var_dump($exist);
        if(empty($exist)){
            $sql = "insert into focus(uid,fid) VALUE('{$uid}','{$fid}')";
            if($model->execute($sql)) $this->success('取消成功',U("Index/people/index/id/$fid"),2);
        }else{
            $sql = "delete from focus WHERE fid = '{$fid}' AND uid = '{$uid}'";
            if($model->execute($sql)) $this->success('取消成功',U("Index/people/index/id/$fid"),2);
        }

    }

}